#include <iostream>
#include "opencv2/opencv.hpp"
// #include "opencv2/gpu/gpu.hpp"
#include "opencv2/core/cuda.hpp"
using namespace std;


int main (int argc, char* argv[])
{
    clock_t time1,time2;
    double t;
    cv::Mat src_host,dst_cpu,result_host;
    cv::cuda::GpuMat dst, src;
    string image_path;

        
    try
    {
        for(int i=1; i<4 ;i++ ){
        image_path = "../../data/image"+to_string(i)+".jpg";
        cout<<image_path<<endl;
        time1 = clock();
        src_host = cv::imread(image_path, CV_LOAD_IMAGE_GRAYSCALE);
        time2 = clock();
        t = ((double)(time2 - time1)) / CLOCKS_PER_SEC ;
        cout << "Process imread running time: " << t << "s" << endl;
        time1 = clock();
        cv::threshold(src_host, dst_cpu, 128.0, 255.0, CV_THRESH_BINARY);
        time2 = clock();
        t = ((double)(time2 - time1)) / CLOCKS_PER_SEC ;
        cout << "Process threshold cpu running time: " << t << "s" << endl;
        time1 = clock();
       
        src.upload(src_host);
        time2 = clock();
        t = ((double)(time2 - time1)) / CLOCKS_PER_SEC ;
        cout << "Process upload running time: " << t << "s" << endl;
        time1 = clock();

        cv::cuda::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);
        
        time2 = clock();
        t = ((double)(time2 - time1)) / CLOCKS_PER_SEC ;
        cout << "Process threshold gpu running time: " << t << "s" << endl;
        time1 = clock();
        dst.download(result_host);

        time2 = clock();
        t = ((double)(time2 - time1)) / CLOCKS_PER_SEC ;
        cout << "Process download gpu running time: " << t << "s" << endl;
        time1 = clock();
        cout << ""<<endl;
        cv::imshow("Result", result_host);
        cv::waitKey();
        }

    }
    catch(const cv::Exception& ex)
    {
        std::cout << "Error: " << ex.what() << std::endl;
    }
    return 0;
}